package logic

import (
	"context"
	"fmt"
	"taobao-merchandis/taobaorpc/models/mysql"

	"taobao-merchandis/taobaorpc/internal/svc"
	"taobao-merchandis/taobaorpc/taobaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *taobaorpc.UserLoginRequest) (*taobaorpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在,请前去注册")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误,请重新输入")
	}
	return &taobaorpc.UserLoginResponse{
		UserId: int64(user.ID),
	}, nil
}
